package com.car.advertising.view;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import com.car.advertising.R;


/**
 * 类说明： 自定义获取短信验证码按钮
 * 
 * @author fangke
 * @date 2014-8-4
 * @version 1.0
 */
public class GetCodeButton extends Button {

	private CountDownTimer countDownTimer;
	private static int times = 0;

	public GetCodeButton(Context context, AttributeSet attrs) {
		super(context, attrs);

		countDownTimer = new CountDownTimer(60000, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				// TODO Auto-generated method stub
				setText(String.format("%ds后重发", millisUntilFinished / 1000));
				setEnabled(false);
				setBackgroundResource(R.drawable.shape_corner4);
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				setText("重发验证码");
				setEnabled(true);
				setBackgroundResource(R.drawable.shape_corner3);
			}
		};

		reset();
		setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (times <= 3) {
					countDownTimer.start();
					times++;
				}
			}
		});
	}

	public void startCountDown() {
		if (times <= 3) {
			countDownTimer.start();
			times++;
		}
	}

	public void reset() {
		countDownTimer.cancel();
		setEnabled(true);
		times = 0;
		setBackgroundResource(R.drawable.shape_corner3);
		setText("获取验证码");
	}

}
